#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#include<sys/stat.h>

int main(){
    umask(0);//将当前进程的umask设置为0
    int fd=open("./tmp.txt",O_RDWR|O_CREAT,0664);//权限必须要写
    if(fd<0){
        perror("open error");
        return -1;
    }
    char* data="还有一个月!!!\n";
    ssize_t ret=write(fd,data,strlen(data));
    if(ret<0){
        perror("write error");
        close(fd);
        return -1;
    }
    //if(ret!=strlen(data)){
    //    write(fd,data+ret,strlen(data)-ret);//从上次没写入的部分继续写入
    //}
    lseek(fd,0,SEEK_SET);
    char buf[1024]={0};
    ret=read(fd,buf,1023);
    if(ret<0){
        perror("read error");
        close(fd);
        return -1;
    }
    else if(ret==0){
        printf("end of file!!!\n");
        close(fd);
        return -1;
    }
    printf("%s\n",buf);
    close(fd);
    return 0;
}
